from google.appengine.ext import db, search
import json, os

class InventionUser(db.Model):
    user = db.UserProperty
    institution = db.StringProperty()

def codeSampleDict(sample):
      return {'key' : str(sample.key()),
              'name' : sample.name,
              'lbcodefile' : sample.lbcodefile,
              'lbimage' : sample.lbimage,
              'pbcodefile' : str(sample.pbcodefile),
              'pbimage' : sample.pbimage,
              'textfile' : str(sample.textfile),
              'desc' : str(sample.desc),
              'author' : str(sample.author),
              'creationdate' : str(sample.creationdate),
              'id1' : sample.id1}

def themeDict(sample):
      return {'name' : sample.name,
              'image' : sample.image,
              'workshopIds' : ', '.join(sample.workshopIds)}

def materialDict(sample):
      return {'name' : sample.name,
              'cost' : sample.cost,
              'where' : sample.where,
              'tips' : str(sample.tips),
              'image' : sample.image,
              'type' : sample.type}

def workshopDict(workshop):
    return {'key' : str(workshop.key()),
          'codeId' : " ".join(workshop.codeId).replace('\x0b', '').replace('\x1d', ' '),
          'insp' : str(workshop.insp),
#          'age1' : workshop.age1,
#          'age2' : workshop.age2,
#          'ageAvg' : workshop.ageAvg,
#          'cost' : workshop.cost,
          'facilitators' : str(workshop.facilitators),
          'longDesc' : str(workshop.longDesc),
#          'time' : str(workshop.time),
          'material' : ", ".join(workshop.craft).replace('\x0b', '').replace('\x1d', ' ') + ' ' +
          ", ".join(workshop.hardware).replace('\x0b', '').replace('\x1d', ' ') + ' ' + 
          ", ".join(workshop.software).replace('\x0b', '').replace('\x1d', ' '),
          'recordId' : workshop.recordId,
          'date' : str(workshop.date),
          'shortDesc' : str(workshop.shortDesc),
          'title' : workshop.title,
          'photoFolder' : workshop.photoFolder,
          'photos' : ', '.join(workshop.photos),
          'location' : workshop.location} 

def relationalWorkshopsToJSON(workshops):
    jsonWorkshops = []
    for workshop in workshops:
      jsonWorkshops.append(workshopDict(workshop))
    return jsonWorkshops

def codeSamplesToJSON(codeSamples):
    jsonCodeSamples = []
    for sample in codeSamples:
        dict = codeSampleDict(sample)
        jsonCodeSamples.append(dict)
        dict['workshops'] = relationalWorkshopsToJSON(sample.workshops())
    return jsonCodeSamples

def materialsToJSON(materials):
    jsonMaterials = []
    for material in materials:
        dict = materialDict(material)
        jsonMaterials.append(dict)
        dict['workshops'] = relationalWorkshopsToJSON(material.workshops())
    return jsonMaterials

def themesToJSON(themes):
    jsonThemes = []
    for theme in themes:
        dict = themeDict(theme)
        jsonThemes.append(dict)
        dict['workshops'] = relationalWorkshopsToJSON(theme.workshops())
    return jsonThemes

def relationalCodeSamplesToJSON(codeSamples):
    jsonCodeSamples = []
    for sample in codeSamples:
        dict = codeSampleDict(sample)
        jsonCodeSamples.append(dict)
    return jsonCodeSamples

def relationalMaterialsToJSON(materials):
    jsonMaterials = []
    for material in materials:
        dict = materialDict(material)
        jsonMaterials.append(dict)
    return jsonMaterials

def relationalThemesToJSON(themes):
    jsonThemes = []
    for theme in themes:
        dict = themeDict(theme)
        jsonThemes.append(dict)
    return jsonThemes

def workshopsToJSON(workshops):
    jsonWorkshops = []
    for workshop in workshops:
        dict = workshopDict(workshop)
        dict['codeSamples'] = relationalCodeSamplesToJSON(workshop.codesamples())
        dict['themes'] = relationalThemesToJSON(workshop.themes())
        dict['materials'] = relationalMaterialsToJSON(workshop.materials())
        jsonWorkshops.append(dict)
    return jsonWorkshops
    
class Material(search.SearchableModel):
    name = db.StringProperty()
    cost = db.StringProperty()
    where = db.StringProperty()
    tips = db.TextProperty()
    image = db.StringProperty()
    type = db.StringProperty()
    datetime = db.DateTimeProperty(auto_now=True)
    def doprint(self):
        print 'name: ', self.name
        print 'cost: ', self.name
        print 'where: ', self.where
        print 'tips: ', self.tips
        print 'image: ', self.image
        print 'type: ', self.type
        print 'datetime: ', self.datetime
        print
        
    def workshops(self):
        return (x.workshop for x in self.materialrelationalentry_set)

class Theme(search.SearchableModel):
    name = db.StringProperty()
    image = db.StringProperty()
    workshopIds = db.StringListProperty()
    datetime = db.DateTimeProperty(auto_now=True)
    def doprint(self):
        print 'name: ', self.name
        print 'image: ', self.image
        print 'workshopIds: ', self.workshopIds
        print 'datetime: ', self.datetime
        print
    def workshops(self):
        return (x.workshop for x in self.themerelationalentry_set)

class CodeSample(search.SearchableModel):
    name = db.StringProperty()
    lbcodefile = db.StringProperty()
    lbimage = db.StringProperty()
    pbcodefile = db.StringProperty()
    pbimage = db.StringProperty()
    textfile = db.StringProperty()
    author = db.StringProperty()
    creationdate = db.StringProperty()
    desc = db.TextProperty()
    id1 = db.IntegerProperty()
    datetime = db.DateTimeProperty(auto_now=True)
    def doprint(self):
        print 'name: ', self.name
        print 'logoblocks code: ', self.lbcodefile
        print 'logoblocks image: ', self.lbimage
        print 'pico blocks code: ', self.pbcodefile
        print 'pico blocks image: ', self.pbimage
        print 'logo code: ', self.textfile
        print 'desc: ', self.desc
        print 'id1: ', self.id1
        print 'author: ', self.author
        print 'creationdate: ', self.creationdate
        print 'datetime: ', self.datetime
        print
        
    def workshops(self):
        return (x.workshop for x in self.relationalentry_set)

class Image(db.Model):
    path = db.StringProperty()
    im = db.BlobProperty()

class Workshop(search.SearchableModel):
    #codeId = db.StringProperty(str)
    codeId = db.StringListProperty()
    insp = db.TextProperty()
#    age1 = db.StringProperty()
#    age2 = db.StringProperty()
#    ageAvg = db.StringProperty()
#    cost = db.StringProperty()
    facilitators = db.TextProperty()
    longDesc = db.TextProperty()
#    time = db.TextProperty()
    craft = db.StringListProperty()
    hardware = db.StringListProperty()
    software = db.StringListProperty()
    recordId = db.StringProperty()
    date = db.StringProperty()
    shortDesc = db.TextProperty()
    title = db.StringProperty()
    photoFolder = db.StringProperty()
    photos = db.StringListProperty()
    location = db.StringProperty()
    datetime = db.DateTimeProperty(auto_now=True)
    def doprint(self):
         print 'title: ', self.title
         print 'codeId: ', self.codeId
         print 'insp: ', self.insp
         print 'longDesc: ', self.longDesc
#         print 'time:', self.time
         print 'date:', self.date
         print 'shortDesc:', self.shortDesc
         print 'photoFolder:', self.photoFolder
         print 'recordId:', self.recordId
         print 'photoFolder:', self.photoFolder
         print 'photos:', self.photos
         print 'datetime:', self.datetime
#         print 'age1:', self.age1
#         print 'age2:', self.age1
#         print 'ageAvg:', self.ageAvg
#         print 'cost:', self.cost
         print 'craft:', self.craft
         print 'hardware:', self.hardware
         print 'software:', self.software
         print 'facilitators:', self.facilitators
         
         print
    def codesamples(self):
        return (x.codesample for x in self.relationalentry_set)
    def themes(self):
        return (x.theme for x in self.themerelationalentry_set)
    def materials(self):
        return (x.material for x in self.materialrelationalentry_set)

class RelationalEntry(db.Model):
   workshop = db.ReferenceProperty(Workshop)
   codesample = db.ReferenceProperty(CodeSample)
   
class ThemeRelationalEntry(db.Model):
   workshop = db.ReferenceProperty(Workshop)
   theme = db.ReferenceProperty(Theme)
   
class MaterialRelationalEntry(db.Model):
   workshop = db.ReferenceProperty(Workshop)
   material = db.ReferenceProperty(Material)      